package edu.zqu.instructionalMatters.project.controller;

import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.instructionalMatters.project.VO.CurriculumContentResultVO;
import edu.zqu.instructionalMatters.project.entity.CurriculumContentResult;
import edu.zqu.instructionalMatters.project.mapper.CurriculumContentResultMapper;
import edu.zqu.instructionalMatters.project.service.CollegeService;
import edu.zqu.instructionalMatters.project.service.CourseService;
import edu.zqu.instructionalMatters.project.service.InstructionClassService;
import edu.zqu.instructionalMatters.project.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 何文东
 * @Date 2020-07-03
 * @Description: 课表实际授课内容 - 前端控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("/api")
@Api(tags = "课表实际授课内容")
public class CurriculumContentResultController {
    @Autowired
    private CurriculumContentResultMapper curriculumContentResultMapper;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private UserService userService;
    @Autowired
    private InstructionClassService instructionClassService;

    @ApiOperation(value = "获取课表实际授课内容列表")
    @RequestMapping(value = "/curriculumContentResults", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "curriculumContentPlanId", value = "课程计划授课内容id", paramType = "query", required = false),
            @ApiImplicitParam(name = "courseLearningScheduleId", value = "课程学习计划id", paramType = "query", required = false),
            @ApiImplicitParam(name = "curriculumId", value = "课表id", paramType = "query", required = false),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = false),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = false),
            @ApiImplicitParam(name = "instructionClassId", value = "授课班级id", paramType = "query", required = false)
    })
    public ZQUResult getList(BigInteger collegeId,
                             BigInteger curriculumContentPlanId,
                             BigInteger courseLearningScheduleId,
                             BigInteger curriculumId,
                             BigInteger courseId,
                             BigInteger teacherId,
                             BigInteger instructionClassId
    ) {
        CurriculumContentResult curriculumContentResult1 = new CurriculumContentResult()
                .setCollegeId(collegeId).setCurriculumContentPlanId(curriculumContentPlanId)
                .setCourseLearningScheduleId(courseLearningScheduleId).setCurriculumId(curriculumId)
                .setCourseId(courseId).setTeacherId(teacherId).setInstructionClassId(instructionClassId);               //
        List<CurriculumContentResult> list1 = curriculumContentResultMapper.findAll(curriculumContentResult1);          //
        List<CurriculumContentResultVO> list = new ArrayList<>();                                                       //
        for (CurriculumContentResult curriculumContentResult : list1) {                                                  //
            CurriculumContentResultVO curriculumContentResultVO = new CurriculumContentResultVO();                      //
            BeanUtils.copyProperties(curriculumContentResult, curriculumContentResultVO);                                //
            String collegeName = collegeService.getCollegeName(curriculumContentResult.getCollegeId());                 //获取学校名称
            String courseName = courseService.getCourseName(curriculumContentResult.getCourseId());                     //获取课程名称
            String teacherName = userService.getUserName(curriculumContentResult.getTeacherId());                       //获取教师名称
            String instructionClassName = instructionClassService
                    .getInstructionClassName(curriculumContentResult.getInstructionClassId());                          //获取授课班级名称
            curriculumContentResultVO.setCollegeName(collegeName);                                                      //设置学校名称
            curriculumContentResultVO.setCourseName(courseName);                                                        //设置课程名称
            curriculumContentResultVO.setTeacherName(teacherName);                                                      //设置教师名称
            curriculumContentResultVO.setInstructionClassName(instructionClassName);                                    //设置授课班级名称
            list.add(curriculumContentResultVO);                                                                        //加入到 list
        }
        return ZQUResult.success(list);                                                                                 //成功返回 list
    }

    @ApiOperation(value = "获取课表实际授课内容信息", response = CurriculumContentResultVO.class)
    @GetMapping(value = "/curriculumContentResult/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) {
        CurriculumContentResult curriculumContentResult = curriculumContentResultMapper.selectById(id);        //获取课表计划授课内容对象
        String collegeName = collegeService.getCollegeName(curriculumContentResult.getCollegeId());            //获取学校名称
        String courseName = courseService.getCourseName(curriculumContentResult.getCourseId());                //获取课程名称
        String teacherName = userService.getUserName(curriculumContentResult.getTeacherId());                  //获取教师名称
        String instructionClassName = instructionClassService
                .getInstructionClassName(curriculumContentResult.getInstructionClassId());                     //获取授课班级名称
        CurriculumContentResultVO curriculumContentResultVO = new CurriculumContentResultVO();                 //初始化VO
        BeanUtils.copyProperties(curriculumContentResult, curriculumContentResultVO);                           //对象复制赋值
        curriculumContentResultVO.setCollegeName(collegeName);                                                 //设置学校名称
        curriculumContentResultVO.setCourseName(courseName);                                                   //设置课程名称
        curriculumContentResultVO.setTeacherName(teacherName);                                                 //设置教师名称
        curriculumContentResultVO.setInstructionClassName(instructionClassName);                               //设置授课班级名称
        return new ZQUResult(HttpStatus.SUCCESS, "请求成功", curriculumContentResultVO);                 //成功返回对象数据
    }

    @ApiOperation(value = "添加")
    @RequestMapping(value = "/curriculumContentResult", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "curriculumContentPlanId", value = "课程计划授课内容id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseLearningScheduleId", value = "课程学习计划id", paramType = "query", required = true),
            @ApiImplicitParam(name = "curriculumId", value = "课表id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = true),
            @ApiImplicitParam(name = "instructionClassId", value = "授课班级id", paramType = "query", required = true),
            @ApiImplicitParam(name = "result", value = "结果", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult add(BigInteger collegeId,
                         BigInteger curriculumContentPlanId,
                         BigInteger courseLearningScheduleId,
                         BigInteger curriculumId,
                         BigInteger courseId,
                         BigInteger teacherId,
                         BigInteger instructionClassId,
                         Integer result,
                         BigInteger byWho) {
        CurriculumContentResult curriculumContentResult = new CurriculumContentResult();               //初始化
        curriculumContentResult.setCollegeId(collegeId);                                               //设置学校id
        curriculumContentResult.setCurriculumContentPlanId(curriculumContentPlanId);                   //设置课程计划授课内容id
        curriculumContentResult.setCourseLearningScheduleId(courseLearningScheduleId);                 //设置课程学习计划id
        curriculumContentResult.setCurriculumId(curriculumId);                                         //设置课表id
        curriculumContentResult.setCourseId(courseId);                                                 //设置课程id
        curriculumContentResult.setTeacherId(teacherId);                                               //设置教师id
        curriculumContentResult.setInstructionClassId(instructionClassId);                             //设置授课班级id
        curriculumContentResult.setResult(result);                                                     //设置结果
        curriculumContentResult.setByWho(byWho);                                                       //设置操作者
        curriculumContentResultMapper.insert(curriculumContentResult);                                 //执行插入
        return ZQUResult.success();                                                                    //操作成功返回
    }

    @ApiOperation(value = "更新")
    @RequestMapping(value = "/curriculumContentResult/{id}", method = RequestMethod.PUT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true),
            @ApiImplicitParam(name = "curriculumContentPlanId", value = "课程计划授课内容id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseLearningScheduleId", value = "课程学习计划id", paramType = "query", required = true),
            @ApiImplicitParam(name = "curriculumId", value = "课表id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = true),
            @ApiImplicitParam(name = "instructionClassId", value = "授课班级id", paramType = "query", required = true),
            @ApiImplicitParam(name = "result", value = "结果", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult update(BigInteger collegeId,
                            @PathVariable BigInteger id,
                            BigInteger curriculumContentPlanId,
                            BigInteger courseLearningScheduleId,
                            BigInteger curriculumId,
                            BigInteger courseId,
                            BigInteger teacherId,
                            BigInteger instructionClassId,
                            Integer result,
                            BigInteger byWho) {
        CurriculumContentResult curriculumContentResult = curriculumContentResultMapper.selectById(id);//先查出旧记录
        curriculumContentResult.setByWho(byWho);                                                       //修改操作者
        curriculumContentResultMapper.updateById(curriculumContentResult);                             //更新操作者
        curriculumContentResultMapper.deleteById(id);                                                  //逻辑删除
        curriculumContentResult = new CurriculumContentResult();                                       //初始化
        curriculumContentResult.setCollegeId(collegeId);                                               //设置学校id
        curriculumContentResult.setCurriculumContentPlanId(curriculumContentPlanId);                   //设置课程计划授课内容id
        curriculumContentResult.setCourseLearningScheduleId(courseLearningScheduleId);                 //设置课程学习计划id
        curriculumContentResult.setCurriculumId(curriculumId);                                         //设置课表id
        curriculumContentResult.setCourseId(courseId);                                                 //设置课程id
        curriculumContentResult.setTeacherId(teacherId);                                               //设置教师id
        curriculumContentResult.setInstructionClassId(instructionClassId);                             //设置授课班级id
        curriculumContentResult.setResult(result);                                                     //设置结果
        curriculumContentResult.setLastVersion(id);                                                    //设置上一版本
        curriculumContentResult.setByWho(byWho);                                                       //设置操作者
        curriculumContentResultMapper.insert(curriculumContentResult);                                 //执行插入
        return ZQUResult.success();                                                                    //操作成功返回
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/curriculumContentResult/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id,
                            BigInteger byWho) {
        CurriculumContentResult curriculumContentResult = curriculumContentResultMapper.selectById(id);//先查出旧记录
        curriculumContentResult.setByWho(byWho);                                                       //修改操作者
        curriculumContentResultMapper.updateById(curriculumContentResult);                             //更新操作者
        curriculumContentResultMapper.deleteById(id);                                                  //逻辑删除
        return ZQUResult.success();                                                                    //操作成功返回
    }
}

